package oom;


import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * -Xmx20m -XX:MaxDirectMemorySize=10M -XX:+HeapDumpOnOutOfMemoryError
 */
public class DirectMemoryDemo1 {
    final static int M1 = 1024 * 1024;
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        Unsafe unsafe = (Unsafe) theUnsafe.get(null);

        List list = new ArrayList();
        for (;;) {
            ByteBuffer buffer = ByteBuffer.allocateDirect(M1);
            list.add(buffer);
        }
    }
}
